/**
 * 
 */
package forum;

import java.util.ArrayList;
import java.util.List;

import database.DBHandler;

/**
 * @author Olga
 * 
 */
public class Forum {
	private int ID;
	private String subject;
	private List<Thread> threads;

	public Forum(int ID, String subject, DBHandler dbHandler) {
		this.ID = ID;
		this.subject = subject;
		this.threads = new ArrayList<Thread>();
	}
	
	public Forum(int ID, String subject) {
		this.ID = ID;
		this.subject = subject;
		this.threads = new ArrayList<Thread>();
	}

	/**
	 * 
	 * @param subject
	 * @param content
	 * @param author
	 * @param rootPostID
	 * @return true on success, false if the thread with current subject is
	 *         already exist
	 */
	public synchronized boolean addThread(Thread toAdd) {
		if (!this.isExist(toAdd.getRoot().getSubject())) {
			return this.threads.add(toAdd);
		}
		return false;
	}

	/**
	 * 
	 * @param subject
	 * @return true if the thread with such a subject is already exist
	 */
	private synchronized boolean isExist(String subject) {
		for (Thread t : this.threads) {
			if (t.getRoot().getSubject().equalsIgnoreCase(subject))
				return true;
		}
		return false;
	}

	/**
	 * 
	 * @param postID
	 *            II of post to add the reply to
	 * @param subject
	 * @param content
	 * @param author
	 * @param newPostID
	 *            - new generated postID
	 * @return true on success. false if post with postID was not found.
	 */
	public synchronized int addReply(int postID, String subject,
		String content, String author, int newPostID) {
		Post post = null;
		for (Thread t : this.threads) {
			if ((post = t.getPost(postID)) != null) {
				break;
			}
		}
		if (post != null) {
			Post newReply = new Post(newPostID, subject, content, post, "", author);
			if(post.addReply(newReply)) {
				return newReply.getID();
			}
		} 
		return -1;
	}

	/**
	 * @param username
	 * @param postID
	 *            - postToEdit
	 * @param content
	 * @return true on success. <br>
	 *         false if post with postID was not found <br>
	 *         or user is not a user that published this post
	 */
	public synchronized boolean editPost(String username, int postID,
			String content) {
		Post toEdit = null;
		for (Thread t : this.threads) {
			if ((toEdit = t.getPost(postID)) != null) {
				if (toEdit.getAuthor().equals(username))
					return toEdit.editPost(content);
				else {
					return false;
				}
			}
		}
		return false;
	}

	/**
	 * if postID is one of root posts of threads, redirect to method remove
	 * thread
	 * 
	 * @param postID
	 * @return true on success, false else
	 */
	public synchronized boolean removePost(int postID) {

		for (Thread t : this.threads) {
			// if post with postID is the root post of thread, redirect do
			// removeThread method
			if (t.getID() == postID)
				return this.removeThread(postID);
			// search in the tree of thread
			else if (t.getPost(postID) != null)
				return t.removePost(postID);
		}
		
		return false;
	}

	/**
	 * @param rootPostID
	 * @return true on success, else false
	 */
	public synchronized boolean removeThread(int rootPostID) {
		for (int i = 0; i < this.threads.size(); i++)
			if (this.threads.get(i).getID() == rootPostID) {
				return (this.threads.remove(i) != null);
			}
		return false;
	}

	/**
	 * @return the subject
	 */
	public synchronized String getSubject() {
		return subject;
	}

	/**
	 * @param subject
	 *            the subject to set
	 */
	public synchronized void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * @return the iD
	 */
	public synchronized int getID() {
		return ID;
	}

	/**
	 * @return the threads
	 */
/*
	public synchronized List<Thread> getThreads() {
		List<Thread> ts = new ArrayList<Thread>();
		ts = dbHandler.getThreads(getID());
		return ts;
	}
*/
        /**
	 * @param threads
	 *            the threads to add
	 */
	public void addThread4GUI(Thread thread) {
		this.threads.add(thread);
	}

	/**
	 * 
	 * @param rootPostID
	 * @return thread with ID rootPostID if exist, else return null
	 */
	public synchronized Thread getThread(int rootPostID) {
		for (Thread t : this.threads)
			if (t.getID() == rootPostID) {
				return t;
			}
		return null;
	}

	/**
	 * 
	 * @param postID
	 * @return reference to post with postID. if not found, null returned.
	 */
	public synchronized Post findPost(int postID) {
		Post ans = null;
		for (Thread t : this.threads)
			if ((ans = t.getPost(postID)) != null)
				break;
		return ans;
	}

}
